package com.tyylab.message.core.service;

import io.lettuce.core.api.StatefulRedisConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class UniqueMessageSupportService implements IUniqueSupport {
    @Autowired
    @Qualifier("readConn")
    StatefulRedisConnection<String,String> redisConnection;

    @Autowired
    @Qualifier("writeConn")
    StatefulRedisConnection<String,String> writeConn;

    @Override
    public String getExistMsgId(String channel, String hashKey) {
        String region = getChannelKey(channel);
        return redisConnection.sync().hget(region,hashKey);
    }

    @Override
    public void addUniqueKey(String channel, String hashKey, String msgId) {
        String region = getChannelKey(channel);
        writeConn.sync().hset(region,hashKey,msgId);
    }

    @Override
    public void removeUniqueKey(String channel, String hashkey) {
        String region = getChannelKey(channel);
        writeConn.sync().hdel(region,hashkey);
    }

    private String getChannelKey(String channel){
        return channel + ":unique";
    }
}
